<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>学生管理系统</title>
    <script src="/js/jquery-3.7.1.min.js"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="search-box" style="display: flex;">
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" style="width: 115px; height: 45px;margin-top:5px;" onclick="modalShow()">
            新增学生
        </button>
        <select class="form-control" id="searchType" name="searchType" style="width: 80px; height: 45px; margin-top:5px;margin-bottom:5px;margin-left:700px">
            <option value="name">姓名</option>
            <option value="id">编号</option>
        </select>
        <input type="text" class="form-control" style="width: 300px; height: 45px; margin-top:5px;margin-bottom:5px;margin-left: 10px;"  placeholder="请输入学生姓名或学号" id="searchBox">
        <button type="button" class="btn btn-primary" style="width: 75px; height: 45px; margin-top:5px;margin-bottom:5px;margin-left: 5px;" onclick="searchStudent();">查询</button>
    </div>
    <table class="table table-bordered" style="text-align: center;">
        <thead>
        <tr>
            <th style="width: 100px;text-align: center;">编号</th>
            <th style="width: 200px;text-align: center;">学号</th>
            <th style="width: 100px;text-align: center;">姓名</th>
            <th style="width: 100px;text-align: center;">性别</th>
            <th style="width: 100px;text-align: center;">年龄</th>
            <th style="width: 100px;text-align: center;">分数</th>
            <th style="width: 100px;text-align: center;">编辑操作</th>
            <th style="width: 100px;text-align: center;">删除操作</th>
        </tr>
        </thead>
        <tbody id="students">
        </tbody>
    </table>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">学生信息</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="modalClose()"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">

                <form id="formStudent">
                    <input hidden="hidden" id="id" />
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no" name="no" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <!--   下拉列表     -->
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄">
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩">
                    </div>
                </form>
                <!--       表单结束         -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="addStudent();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
    function modalShow() {
        $('#myModal').modal('show');
    }
    function modalClose() {
        $('#myModal').modal('hide');
    }
    function addStudent() {
        var data = $("#formStudent").serialize();
        var id = $("#id").val();
        if (id) {
            // 如果id存在，则执行更新操作
            $.ajax({
                url: "/api/student/update",
                method: "put",
                data: data + "&id=" + id,  // 将id作为数据参数发送到后端
            }).done(function () {
                loadTable();
                $('#myModal').modal('hide');
            });
        } else {
            // 如果id不存在，则执行新建操作
            $.ajax({
                url: "/api/student/insert",
                method: "post",
                data: data
            }).done(function () {
                loadTable();
                $('#myModal').modal('hide');
            });
        }
    }
    function searchStudent() {
        var searchTerm = $("#searchBox").val();  // 获取搜索框中的输入值
        var searchType = $("#searchType").val();  // 获取搜索类型的值
        $.ajax({
            url: "/api/student/search",
            method: "get",
            data: {term: searchTerm, type: searchType},  // 将搜索词作为参数发送到后端
        }).done(function (data) {
            updateTable(data);  // 调用更新表单函数，更新表单显示为查询到的数据
        });
    }
    function updateTable(data) {
        var len = data.length;
        var html = "";
        for (var i = 0; i < len; i++) {
            var item = data[i];
            html += "<tr>"
                + "<td>" + item.id + "</td>"
                + "<td>" + item.no + "</td>"
                + "<td>" + item.name + "</td>"
                + "<td>" + item.sex + "</td>"
                + "<td>" + item.age + "</td>"
                + "<td>" + item.score + "</td>"
                + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a></td>"
                + "<td><a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"
                + "<tr>";
        }
        $("#students").html(html);  // 更新表格数据
    }
    function editStudent(id) {
        $('#myModal').modal('show');
        $.ajax({
            url: "/api/student/get/" + id
        }).done(function (data) {
            $("#id").val(data.id);
            $("#no").val(data.no);
            $("#name").val(data.name);
            $("#password").val("");
            $("#sex").val(data.sex);
            $("#age").val(data.age);
            $("#score").val(data.score);
        });
    }
    function deleteStudent(id) {
        var data = $("#formStudent").serialize();
        if (confirm("确定要删除该学生吗？")) {
            $.ajax({
                url: "/api/student/delete",
                method: "delete",
                data: data + "&id=" + id,
            }).done(function () {
                loadTable();
                $('#myModal').modal('hide');
            });
        }
    }
    function loadTable() {
        $.ajax({
            url: '/api/student/list'
        }).done(function (data) {
            var len = data.length;
            var html = "";
            for (var i = 0; i < len; i++) {
                var item = data[i];
                html += "<tr>"
                    + "<td>" + item.id + "</td>"
                    + "<td>" + item.no + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.sex + "</td>"
                    + "<td>" + item.age + "</td>"
                    + "<td>" + item.score + "</td>"
                    + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a></td>"
                    + "<td><a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"  // 添加删除按钮
                    + "<tr>";
            }
            $("#students").html(html);
            console.log(data);
        });
    }
    $(function () {
        loadTable();
    });
</script>
</body>
</html>